﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text.RegularExpressions;

namespace Qy.Web.Authorize.Common;
/// <summary>
/// HttpContext扩展类
/// </summary>
public static partial class HttpContextExtension
{
    /// <summary>
    /// 是否是ajax请求
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    public static bool IsAjaxRequest(this HttpRequest request)
    {
        if (request == null)
        {
            throw new ArgumentNullException(nameof(request));
        }
        return request.Headers["X-Requested-With"] == "XMLHttpRequest" || request.Headers != null && request.Headers["X-Requested-With"] == "XMLHttpRequest";
    }

    /// <summary>
    /// 获取客户端IP
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    public static string GetClientUserIp(this HttpContext context)
    {
        if (context == null) return "";
        var result = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
        if (string.IsNullOrEmpty(result))
        {
            result = context.Connection.RemoteIpAddress?.ToString();
        }
        if (string.IsNullOrEmpty(result) || result.Contains("::1"))
            result = "127.0.0.1";

        result = result.Replace("::ffff:", "127.0.0.1");
        result = IsIP(result) ? result : "127.0.0.1";
        return result;
    }


    public static bool IsIP(string ip)
    {
        return MyRegex().IsMatch(ip);
    }

    public static int GetUId(this HttpContext context)
    {
        var uid = context.User.FindFirstValue(ClaimTypes.PrimarySid);
        return !string.IsNullOrEmpty(uid) ? int.Parse(uid) : 0;
    }
    public static int GetGId(this HttpContext context)
    {
        var uid = context.User.FindFirstValue(ClaimTypes.PrimaryGroupSid);
        return !string.IsNullOrEmpty(uid) ? int.Parse(uid) : 0;
    }
    public static string GetName(this HttpContext context)
    {
        return context.User?.Identity?.Name;
    }

    public static string GetUserAgent(this HttpContext context)
    {
        return context.Request.Headers["User-Agent"];
    }
    public static string GetController(this HttpContext context)
    {
        return context.Request.RouteValues["Controller"].ToString();
    }
    public static string GetAction(this HttpContext context)
    {
        return context.Request.RouteValues["Action"].ToString();
    }

    /// <summary>
    /// 获取请求令牌
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    public static string GetToken(this HttpContext context)
    {
        return context.Request.Headers["Authorization"];
    }

    /// <summary>
    /// ClaimsIdentity
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    public static IEnumerable<ClaimsIdentity> GetClaims(this HttpContext context)
    {
        return context.User?.Identities;
    }


    public static string GetRequestUrl(this HttpContext context)
    {
        return context != null ? context.Request.Path.Value : "";
    }

    [GeneratedRegex("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$")]
    private static partial Regex MyRegex();
}
